/**
 * @Description:
 * @author 若琳
 * @date 2021/7/1
*/


const express = require('express');
const UserController = express.Router();

//数据集合
const ResultJson = require("../../../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/ResultJson.js").V("v1.0.0");
const UserService = require("../../../../../../../../ruoling-system/src/com/ruoling/system/service/v1/system/service.user.js");
const LayuiGrid = require("../../../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/layuiGrid.js");

/**
 * 用户列表页面
 */
UserController.get("/system/user/view",function(Request,response){

    response.type(".html");
    response.render("admin/system/sysuser/userview.html");
});


/**
 * 用户添加页面
 */
UserController.get("/system/user/add/view",function(Request,response){

    response.type(".html");
    response.render("admin/system/sysuser/useraddview.html");
});

/**
 * 用户编辑页面
 */
UserController.get("/system/user/edit/view",function(Request,response){

    let Id = Request.query.Id;
    UserService.UserRoleIdAll(Id,(err,data)=>{
        response.type(".html");

        response.render("admin/system/sysuser/usereditview.html",{user:data.user,list:data.list});
    });

});

/**
 * 用户添加
 */
UserController.post("/v1/api/system/user/add",function(Request,response){

    try{
        let {sys_loginname,sys_loginpwd,sys_name,rolearr_id} = Request.body;
        UserService.UserAddService(sys_loginname,sys_loginpwd,sys_name,rolearr_id,(err,data)=>{
           if(err){
               response.send(ResultJson.GUBError());
               return;
           }
                response.send(ResultJson.MegOk("用户添加成功"));
                return;
        });

    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 用户删除，批量删除
 */
UserController.post("/v1/api/system/user/delete",function(Request,response){
    let {Id_all}  = Request.body;

    try {
        UserService.UserDeleteAll(Id_all,(err,row)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(ResultJson.MegOk(""));
            return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});

/**
 * 用户编辑
 */
UserController.post("/v1/api/system/user/edit",function(Request,response){
    try{
        let {sys_loginname,sys_name,rolearr_id,ID} = Request.body;
        UserService.UserEditService(sys_loginname,sys_name,rolearr_id,ID,(err,data)=>{
            if(err){
                console.log(err)
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(ResultJson.MegOk("用户修改成功"));
            return;
        });

    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});



/**
 * 用户列表
 */
UserController.get("/v1/api/system/user/list",function(Request,response){

   try{
       let {limit,page,sys_loginname,sys_name,sys_admin_type} = Request.query;
       UserService.User_List(limit,page,sys_loginname,sys_name,sys_admin_type,function (err,data){
           if(err){
               response.send(ResultJson.GUBError());
               return;
           }

               response.send(new LayuiGrid(data.pager.totalRecord,data.pager.dataList));
               return;
       })

   }catch (e){

       response.send(ResultJson.SystemError());
       return;
   }
});




module.exports = UserController;